import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:freedom/util/navigator_provider.dart';

typedef CallBack = void Function(String roomCode);

typedef CloseWindow = void Function();

class CommonAlterDialog extends StatefulWidget {
  final CallBack? callback;
  final Function? closeWindow;
  final EdgeInsets? insertPadding;
  final Widget? child;
  final double? height;
  final double? width;
  final Color? backgroundColor;

  const CommonAlterDialog({
    super.key,
    this.callback,
    this.insertPadding,
    this.child,
    this.height,
    this.width,
    this.backgroundColor,
    this.closeWindow,
  });

  @override
  CommonAlterDialogState createState() => CommonAlterDialogState();
}

class CommonAlterDialogState extends State<CommonAlterDialog>
    with SingleTickerProviderStateMixin {
  AnimationController? controller;
  Animation<double>? widthAnimation;
  Animation<double>? heightAnimation;
  /*  Animation<double>? curvedAnimation; */
  double get height => widget.height ?? 300.h;
  double get width => widget.width ?? 550.h;
  @override
  void initState() {
    controller = AnimationController(
      duration: const Duration(milliseconds: 200), // 动画持续时间
      vsync: this,
    )..addListener(() {
        setState(() {});
      }); // 启动动画

    /*   curvedAnimation = CurvedAnimation(
      parent: controller!,
      curve: Curves.easeInQuad,
    ); */
    widthAnimation = Tween<double>(
      begin: ScreenUtil().screenWidth / 2,
      end: 0,
    ).animate(controller!);

    heightAnimation = Tween<double>(
      begin: ScreenUtil().screenHeight / 2,
      end: 0,
    ).animate(controller!);
    /* ClipRect(
      clipper: _ClipRectClipper(Rect.fromLTWH(
          (widthAnimation?.value ?? 0),
          (heightAnimation?.value ?? 0),
          (ScreenUtil().screenWidth / 2 - (widthAnimation?.value ?? 0)) * 2,
          (ScreenUtil().screenHeight / 2 - (heightAnimation?.value ?? 0)) * 2)), */
    controller?.forward();
    super.initState();
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ClipRect(
      clipper: _ClipRectClipper(Rect.fromLTWH(
          (widthAnimation?.value ?? 0),
          0,
          (ScreenUtil().screenWidth / 2 - (widthAnimation?.value ?? 0)) * 2,
          ScreenUtil().screenHeight)),
      child: Material(
        color: Colors.black54,
        child: SizedBox(
          // 因为内部 margin 了 30.h 所以这里要加上去才是外部的真实高度
          height: double.infinity,
          width: double.infinity,
          child: Center(
              child: SizedBox(
            width: width,
            height: height,
            child: Stack(
              children: [
                Container(
                  height: double.infinity,
                  width: double.infinity,
                  margin: EdgeInsets.all(10.h),
                  decoration: BoxDecoration(
                    border: Border.all(
                        width: 1.0, color: Colors.white.withOpacity(0.5)),
                    boxShadow: [
                      BoxShadow(
                          color: Colors.white.withOpacity(0.2),
                          spreadRadius: 2.0,
                          blurRadius: 2.0),
                    ],
                    borderRadius: BorderRadius.circular(8.0.r),
                  ),
                ),
                ClipRRect(
                  borderRadius: BorderRadius.circular(8.0.r),
                  child: Container(
                      height: double.infinity,
                      width: double.infinity,
                      decoration: BoxDecoration(
                        color: widget.backgroundColor,
                      ),
                      margin: EdgeInsets.all(15.h),
                      child: widget.child ?? Container()),
                ),
                Align(
                    alignment: const Alignment(1, -1),
                    child: InkWell(
                      onTap: () {
                        if (widget.closeWindow != null) {
                          widget.closeWindow!();
                        } else {
                          NavigatorProvider.pop(context: context);
                        }
                      },
                      borderRadius: BorderRadius.circular(40.h),
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(8.0.r),
                        child: Container(
                          height: 30.h,
                          width: 30.h,
                          decoration: BoxDecoration(
                              color: Colors.blue[300],
                              borderRadius: BorderRadius.circular(40.h),
                              border: Border.all(
                                  color: Colors.white.withOpacity(0.3),
                                  width: 3.h)),
                          child: Icon(
                            Icons.local_dining,
                            color: Colors.orange[200],
                            size: 20.h,
                          ),
                        ),
                      ),
                    )),
              ],
            ),
          )),
        ),
      ),
    );
  }
}

class _ClipRectClipper extends CustomClipper<Rect> {
  final Rect _clipRect;

  _ClipRectClipper(this._clipRect);

  @override
  Rect getClip(Size size) {
    // 返回传入的裁剪区域
    return _clipRect;
  }

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) {
    // 当裁剪区域变化时返回true
    return oldClipper != this;
  }
}
